/*
 * Copyright 2010 Nicholas Alexander Cross
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.intellij.lang.plist.fileTypes;

import com.intellij.lexer.Lexer;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.fileTypes.SyntaxHighlighterBase;
import com.intellij.psi.tree.IElementType;
import org.intellij.lang.plist.parser.PListElementType;
import org.intellij.lang.plist.parser.PListElementTypes;
import org.intellij.lang.plist.parser.PListLexer;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

public class PListSyntaxHighlighter extends SyntaxHighlighterBase {
    private PListLexer lexer;

    private final Map<IElementType, TextAttributesKey> colors = new HashMap<IElementType, TextAttributesKey>();
    private final Map<IElementType, TextAttributesKey> backgrounds = new HashMap<IElementType, TextAttributesKey>();


    public PListSyntaxHighlighter() {
        lexer = new PListLexer();

        fillMap(colors, PListElementTypes.DICTIONARY_TOKENS, PListHighlighterColours.DICTIONARY);
        fillMap(colors, PListElementTypes.ARRAY_TOKENS, PListHighlighterColours.ARRAY);

        colors.put(PListElementTypes.BAD_CHARACTER, PListHighlighterColours.BAD_CHARACTER);
        colors.put(PListElementTypes.COMMENT, PListHighlighterColours.COMMENT);
        colors.put(PListElementTypes.STRING, PListHighlighterColours.STRING);
    }

    @NotNull
    public Lexer getHighlightingLexer() {
        return lexer;
    }

    @NotNull
    public TextAttributesKey[] getTokenHighlights(IElementType tokenType) {
        return pack(getAttributeKeys(tokenType, backgrounds), getAttributeKeys(tokenType, colors));
    }

    private TextAttributesKey getAttributeKeys(IElementType tokenType, Map<IElementType, TextAttributesKey> map) {
        TextAttributesKey attributes = map.get(tokenType);
        if (attributes == null && tokenType instanceof PListElementType) {
            return map.get(((PListElementType) tokenType).getParsedType());
        }
        return map.get(tokenType);
    }

}
